package chamans.away
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.methods.EnvMapMethod;
	
	import flash.geom.*;
	
	import jiglib.geometry.JBox;
	import jiglib.plugin.away3d4.Away3D4Mesh;
	
	public class chaise
	{
		private var BASE:Object;
		private var intern:Mesh;
		private var end:Function;
		private var Pos:Vector3D;
		private var mat:BitmapMaterial;
		private var m_liste:Vector.<Mesh>;
		private var r_liste:Vector.<JBox>;
		
		public function chaise(base:Object, END:Function=null, P:Vector3D=null)
		{
			BASE=base; end=END || new Function();
			BASE.LOAD.IMGLIST("assets/demobullet/chair/", ["chair_d", "chair_s", "chair_local" ], initMaterials , ".jpg")	
		}
		private function initMaterials( A:Array  ) : void
		{
			mat = new BitmapMaterial(A[0]);
			mat.ambientColor = BASE.FogColor;
			mat.specularMap = A[1];
			mat.normalMap = A[2];
			mat.lights = BASE.LIGHT;
			
			mat.specular = 2.5;
			mat.ambient=.2;
			mat.gloss =30;
			mat.name="Chair"
			if(BASE.reflect)BASE.F_reflect(mat, .6)
			if(BASE.fog)BASE.F_Fog(mat)
			
			BASE.MatListe.push(mat);
			m_liste = new Vector.<Mesh>();
			r_liste = new Vector.<JBox>();
			BASE.OBJ3D('assets/demobullet/chair/chair.obj', Define);
		}
		private function Define(obj:ObjectContainer3D) : void
		{
			intern = obj.getChildAt(0) as Mesh;
			intern.material=mat;
			end()
		}
		public function clone( p:Vector3D=null, r:int=0 ) : void
		{
			var mesh:Mesh = intern.clone() as Mesh;
			if(p)Pos=p; else Pos=RanPos();
			
			if(BASE.phyOn) Physics(mesh, r ); 
			else {mesh.position=Pos; mesh.rotationY=r}
			
			mesh.material=mat;
			BASE.view.scene.addChild(mesh);
			m_liste.push(mesh);
		}
		private function RanPos() : Vector3D {
			return new Vector3D(BASE.TOOL.Ran(1000, -1000), 43, BASE.TOOL.Ran(1000));
		}
		private function Physics(mesh:Mesh, r:int=0) : void
		{
			var jBox:JBox = new JBox( new Away3D4Mesh(mesh), 38, 45, 86 );
			jBox.friction=.3;
			jBox.restitution=.8;
			jBox.mass=1;
			jBox.moveTo(Pos)
			jBox.rotationY=r;
			r_liste.push(jBox)
			BASE.physics.world.addBody(jBox);
		}
		public function Delete() : void
		{
			if(BASE.phyOn){ while(r_liste.length){
				BASE.physics.world.removeBody(r_liste[r_liste.length-1]);
				r_liste.pop();
			}}
			while(m_liste.length){
				BASE.view.scene.removeChild(m_liste[m_liste.length-1]);
				m_liste.pop();
			}
		}
	}}
